package cn.ifafu.ifafu.util

import org.junit.Assert
import org.junit.Test
import java.text.SimpleDateFormat
import java.util.*

class DateUtilsTest {

    private val format = SimpleDateFormat("yyyy-MM-dd")

    @Test
    fun getCurrentWeek() {
        val opening = parse("2020-09-01 00:00:00")
        val testMap = mapOf(
                -1 to "2020-07-30 12:00:00",
                -1 to "2020-08-29 12:00:00",
                1 to "2020-08-30 12:00:00",
                1 to "2020-08-31 12:00:00",
                1 to "2020-09-01 12:00:00",
                1 to "2020-09-02 12:00:00",
                1 to "2020-09-03 12:00:00",
                1 to "2020-09-04 12:00:00",
                1 to "2020-09-05 12:00:00",
                2 to "2020-09-06 12:00:00",
                2 to "2020-09-07 12:00:00",
                14 to "2020-12-01 12:00:00",
                18 to "2020-12-29 12:00:00",
                18 to "2020-12-30 00:00:00",
                18 to "2020-12-31 00:00:00",
                18 to "2021-01-01 00:00:00",
                18 to "2021-01-02 00:00:00",
                19 to "2021-01-09 00:00:00",
                20 to "2021-01-16 00:00:00",
                21 to "2021-01-23 00:00:00",
                22 to "2021-01-30 00:00:00",
                23 to "2021-02-06 00:00:00",
                24 to "2021-02-13 00:00:00",
                -1 to "2021-02-14 00:00:00",
                -1 to "2021-05-03 00:00:00"
        )
        for (e in testMap.entries) {
            Assert.assertEquals("ERROR: ${e.value}",
                    e.key, DateUtils.getCurrentWeek(opening, parse(e.value)))
        }
    }

    @Test
    fun getCurrentWeekDate() {
        val testMap = mapOf(
                "2020-08-30" to listOf("2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03", "2020-09-04", "2020-09-05"),
                "2020-08-30" to listOf("2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03", "2020-09-04", "2020-09-05"),
                "2020-09-01" to listOf("2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03", "2020-09-04", "2020-09-05"),
                "2020-09-02" to listOf("2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03", "2020-09-04", "2020-09-05"),
                "2020-09-03" to listOf("2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03", "2020-09-04", "2020-09-05"),
                "2020-09-04" to listOf("2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03", "2020-09-04", "2020-09-05"),
                "2020-12-30" to listOf("2020-12-27", "2020-12-28", "2020-12-29", "2020-12-30", "2020-12-31", "2021-01-01", "2021-01-02"),
                "2020-12-31" to listOf("2020-12-27", "2020-12-28", "2020-12-29", "2020-12-30", "2020-12-31", "2021-01-01", "2021-01-02"),
                "2021-01-01" to listOf("2020-12-27", "2020-12-28", "2020-12-29", "2020-12-30", "2020-12-31", "2021-01-01", "2021-01-02"),
                "2021-01-02" to listOf("2020-12-27", "2020-12-28", "2020-12-29", "2020-12-30", "2020-12-31", "2021-01-01", "2021-01-02"),
                "2021-01-03" to listOf("2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06", "2021-01-07", "2021-01-08", "2021-01-09")
        )
        for (e in testMap.entries) {
            val start = parse(e.key)
            Assert.assertEquals("ERROR: ${e.key}",
                    e.value, DateUtils.getCurrentWeekDate(start, "yyyy-MM-dd"))
        }
    }

    @Test
    fun getWeekOffsetDates() {
        val openingDay = Date(parse("2020-09-01"))
        val testMap = mapOf(
//                0 to listOf("2020-08-30", "2020-08-31", "2020-09-01", "2020-09-02", "2020-09-03", "2020-09-04", "2020-09-05"),
                1 to listOf("2020-09-06", "2020-09-07", "2020-09-08", "2020-09-09", "2020-09-10", "2020-09-11", "2020-09-12")
        )
        for (e in testMap.entries) {
            Assert.assertEquals("ERROR: ${e.key}",
                    e.value, DateUtils.getWeekOffsetDates(openingDay, e.key, "yyyy-MM-dd"))
        }
    }


    private fun parse(source: String): Long {
        return format.parse(source)!!.time
    }
}